package com.nervenets.web.controller.handler.admin.movie;

import com.nervenets.utils.JodaUtils;
import com.nervenets.web.controller.handler.SessionHandler;
import com.nervenets.web.entity.MessageCode;
import com.nervenets.web.enumeration.MovieInfoStatus;
import com.nervenets.web.enumeration.MovieStatus;
import com.nervenets.web.enumeration.Role;
import com.nervenets.web.hibernate.domain.film.Movie;
import com.nervenets.web.hibernate.domain.film.MovieCheckChange;
import com.nervenets.web.hibernate.domain.film.MovieCheckInfo;
import com.nervenets.web.hibernate.domain.film.MovieInfo;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

public class MovieCheckHandler extends SessionHandler<MovieCheckParams> {
    @Override
    public ArrayList<Role> allowRoles() {
        return Role.administrator();
    }

    @Override
    public boolean needLog() {
        return false;
    }

    @Override
    public String handle(MovieCheckParams params, HttpServletRequest request) throws Exception {
        final Movie movie = userDao.findById(params.getMovieId(), Movie.class);
        if (null == movie) return errorMessage(MessageCode.code_404, "影片不存在");

        final MovieCheckInfo check = movie.getCheckInfo();
        if (null == check) return errorMessage(MessageCode.code_444, "该项目没有要审核的资料");

        if (!check.getStatus().equals(MovieInfoStatus.checking))
            return errorMessage(MessageCode.code_444, "该项目已审核，不能重复审核");

        if (params.isSuccess()) {
            MovieInfo info = movie.getInfo();
            if (null == info) info = new MovieInfo();

            final long beforePriceOfUnit = info.getPriceOfUnit();
            final MovieStatus beforeStatus = movie.getStatus();

            info.setTitle(check.getTitle());
            info.setSubtitle(check.getSubtitle());
            info.setPriceOfUnit(check.getPriceOfUnit());
            info.setUserLimit(check.getUserLimit());
            info.setDeposit(check.getDeposit());
            info.setCoverImage(check.getCoverImage());
            info.setCarousels(check.getCarousels());
            info.setContent(check.getContent());
            info.setIncomeDetail(check.getIncomeDetail());
            info.setContractDetail(check.getContractDetail());
            info.setRiskWarning(check.getRiskWarning());
            if (beforePriceOfUnit != check.getPriceOfUnit()) {
                info.addPriceChange(check.getUserId(), beforePriceOfUnit, check.getPriceOfUnit());
            }
            if (!beforeStatus.equals(check.getMovieStatus())) {
                info.addStatusChange(check.getUserId(), beforeStatus, check.getMovieStatus());
            }
            movie.setStatus(check.getMovieStatus());
            movie.setReleaseStatus(check.getReleaseStatus());
            movie.setCheckInfo(null);
            movie.setInfo(info);
        } else {
            movie.getCheckInfo().setStatus(MovieInfoStatus.failed);
            movie.getCheckInfo().setFailedReason(params.getFailedReason());
        }

        movie.getCheckChanges().add(new MovieCheckChange(JodaUtils.getTimestamp(), params.getUserId(), params.isSuccess(), params.getFailedReason()));

        userDao.saveOrUpdate(movie);

        globalService.clearRedisMovieStored(movie.getId());

        return successMessage();
    }

    @Override
    public String result() throws Exception {
        return null;
    }
}
